package com.wsc.wisdomchina.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.javaBean.HomeModule;
import com.wsc.wisdomchina.javaBean.ModuleListBean;

import java.util.List;

/**
 * 四大功能适配器
 * Created by ${wangtao} on 2017/2/24.
 */

public class ModuleAdapter extends BaseAdapter {
    private List<ModuleListBean.Module.ModuleItem> objects;
    private Context context;
    private LayoutInflater layoutInflater;
    private boolean isNew;

    public ModuleAdapter(Context context, List<ModuleListBean.Module.ModuleItem> objects, boolean isNew) {
        this.objects = objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.isNew=isNew;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public ModuleListBean.Module.ModuleItem getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_module_view, null);
            int height = Tools.dip2px(context,86);
            convertView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,height));
            convertView.setTag(new ModuleAdapter.ViewHolder(convertView));
        }
        initializeViews((ModuleListBean.Module.ModuleItem) getItem(position), (ModuleAdapter.ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(ModuleListBean.Module.ModuleItem object, ModuleAdapter.ViewHolder holder) {
        //TODO implement
        Glide.with(context).load(object.getIcon())
                .placeholder(R.mipmap.own_pic_logout)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.itemHeadImg);
        if(isNew)
        {
            holder.itemHeadName.setText(object.getName());
        }else
        {
            holder.itemHeadName.setText(object.getName());
        }

    }

    protected class ViewHolder {
        private ImageView itemHeadImg;
        private TextView itemHeadName;

        public ViewHolder(View view) {
            itemHeadImg = (ImageView) view.findViewById(R.id.item_sida_gongnneg_head_img);
            itemHeadName = (TextView) view.findViewById(R.id.item_sida_gongnneg_head_name);
        }
    }
}
